// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import RockingBar from "./RockingBar";

const {ccclass, property} = cc._decorator;

// 受摇杆控制的对象
@ccclass
export default class RockingMove extends cc.Component {


    // onLoad () {}
    @property(RockingBar)
    Rocker: RockingBar = null;
    speed:number =100;
    sx:number = 0;
    sy:number = 1;
    start () {

    }

    // update (dt) {}
    update (dt) {

//移动
        this.node.x += this.sx;
        this.node.y += this.sy;

        if(this.Rocker.dir.mag()<0.5){
            return;
        }

        var vx = this.Rocker.dir.x * this.speed;
        var vy = this.Rocker.dir.y * this.speed;

        this.sx = vx * dt;
        this.sy = vy * dt;

//方向计算
        var r = Math.atan2(this.Rocker.dir.y,this.Rocker.dir.x);
        var degree = r * 180/(Math.PI);
        degree = 360 - degree + 90;
        this.node.rotation = degree;
    }
}
